// pages/search-list/search-list.js
var app = getApp();
var main = require("../../main.js");
const config = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null,
    isGrant: false,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.checkUserAuthorization();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.checkUserAuthorization();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 删除处理
  deleteOnClick: function(e) {
    wx.showLoading({
      title: '加载中...',
    });
    var that = this;
    const s_number = e.currentTarget.dataset.id;
    wx.request({
      url: app.domain_path + app.api_path + '/delete_history',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'post',
      data: {
        token: main.get_storage('token'),
        s_number: s_number
      },
      success: function(res) {
        wx.hideLoading();
        if (!res.data) {
          wx.showToast({
            title: '数据错误',
            icon: 'loading',
            duration: 3000
          })
        }
        if (res.data.RESULT == 'OK') {
          wx.showToast({
            title: "删除成功",
            icon: 'none',
            duration: 3000
          })
          const newList = that.data.list.filter(item => item.s_number !== s_number);
          that.setData({
            list: newList,
          });
        } else {
          wx.showToast({
            title: res.data.MESSAGE,
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: err => {
        wx.hideLoading();
        wx.showToast({
          title: err,
          icon: 'none'
        });
      }
    })
  },

  //跳转详情
  onClick: function(e) {
    const id = e.currentTarget.dataset.id;
    const type = e.currentTarget.dataset.type;
    console.log(id, 'id');
    console.log(type, 'type');
    switch (type) {
      case 1:
        wx.navigateTo({
          url: "/pages/paiyan/data/data?id=" + id
        })
        break;
      case 2:
        wx.navigateTo({
          url: "/pages/paiyan/data1/data?id=" + id
        })
        break;
      case 42:
        wx.navigateTo({ 
          url: "/pages/xitong/data1/data?id=" + id
        })
        break;
      case 4:
        wx.navigateTo({
          url: "/pages/paiyan/data3/data?id=" + id
        })
        break;
      case 5:
        wx.navigateTo({
          url: "/pages/paiyan/data4/data?id=" + id
        })
        break;
      case 6:
        wx.navigateTo({
          url: "/pages/paiyan/data5/data?id=" + id
        })
        break;
      case 7:
        wx.navigateTo({
          url: "/pages/fengguan/data6/data?id=" + id
        })
        break;
      case 8:
        wx.navigateTo({
          url: "/pages/paiyan/data7/data?id=" + id
        })
        break;
      case 9:
        wx.navigateTo({
          url: "/pages/tongfeng/data8/data?id=" + id
        })
        break;
      case 41:
        wx.navigateTo({
          url: "/pages/xitong/data/data?id=" + id
        })
        break;
      case 71:
        wx.navigateTo({
          url: "/pages/fengliang/data/data?id=" + id
        })
        break;
      case 51:
        wx.navigateTo({
          url: "/pages/fengji/data/data?id=" + id
        })
        break;
      case 61:
        wx.navigateTo({
          url: "/pages/zhineng/data/data?id=" + id
        })
        break;
      case 43:
        wx.navigateTo({
          url: "/pages/xitong/data2/data?id=" + id
        })
        break;
      default:
        wx.showToast({
          title: '加载错误',
          icon: 'loading',
          duration: 3000
        })
    }
    
  },

  checkUserAuthorization: function () {
    const that = this;
    // 获取用户的授权信息
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          console.log(main.get_storage('token') != '');
          if(main.get_storage('token') != []){
            //获取个人账户信息
            that.get_member_info();
            //获取计算履历信息
            that.get_smoke_historys();
          }else{
            this.jumpConfirm();
          }
        } else {
          this.jumpConfirm();
        }
      },
      fail: (err) => {
        wx.showToast({
          title: err,
          icon: 'none'
        });
      }
    });
  },
  jumpConfirm:function(){
    wx.showModal({
      title: '温馨提示',
      content: '此功能需要授权登录，请确认~',
      success: (res) => {
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/index/index'
          });
        } else if (res.cancel) {
          wx.switchTab({
            url: '/pages/ser-list/ser-list',
          });
        }
      },
    });
  },

 /**
   * 获取个人账户信息
   */
    get_member_info: function() {
      var that = this;
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.domain_path + app.api_path + '/member_info',
        method: 'post',
        data: {
          token: main.get_storage('token'),
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (!res.data) {
            wx.showToast({
              title: '加载错误',
              icon: 'loading',
              duration: 3000
            })
          }
          if (res.data.RESULT == 'OK') {
            wx.hideLoading();
            that.setData({
              isGrant: true,
              userinfo: res.data.data.member_info,
            })
          } else {
            wx.showToast({
              title: res.data.MESSAGE,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    },

    /**
   * 获取个人账户信息
   */
    get_smoke_historys: function() {
      var that = this;
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.domain_path + app.api_path + '/get_smoke_historys',
        method: 'post',
        data: {
          token: main.get_storage('token'),
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          if (!res.data) {
            wx.showToast({
              title: '加载错误',
              icon: 'loading',
              duration: 3000
            })
          }
          if (res.data.RESULT == 'OK') {
            wx.hideLoading();
            that.setData({
              list: res.data.data.smoke_historys,
            })
          } else {
            wx.showToast({
              title: res.data.MESSAGE,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    },

    index:function(){
      wx.switchTab({  
        url: "/pages/ser-list/ser-list"
      });
    },
})